package cn.pxs.service;

import cn.pxs.entity.LargeFile;
import cn.pxs.entity.User;
import cn.pxs.mapper.UserMapper;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

//用户业务接口实现类
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    LargeFileService largeFileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加用户
    public void add(User user) {
        //加密方式
        PasswordEncoder pe = new BCryptPasswordEncoder();
        user.setPassword(pe.encode(user.getPassword()));
        //创建用户
        user.setRole("P");
        userMapper.add(user);
    }

    @Override
    //根据账号模糊查找用户
    public User findUserByAccount(String account) {
        return userMapper.findUserByAccount(account);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加管理员用户
    public void addAdm(User user) {
        PasswordEncoder pe = new BCryptPasswordEncoder();
        //管理员
        user.setRole("G");
        user.setClosed(0);
        user.setPassword(pe.encode(user.getPassword()));
        userMapper.add(user);
    }

    @Override
    //用户全查询
    public List<User> findAll(Integer start,Integer num) {
        return userMapper.findAll(start, num);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //冻结用户
    public int updateClosed(Integer closed, Integer id) {
        User u = userMapper.findById(id);
        if(u==null){
            return 0;
        }
        userMapper.updateClosed(closed, id);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //重置密码
    public int updatePwd(String password, Integer id,Integer closed) {
        PasswordEncoder pe = new BCryptPasswordEncoder();
        User u = userMapper.findById(id);
        if(u==null){
            return 0;
        }
        userMapper.updatePwd(pe.encode(password),id,closed);
        return 1;
    }

    @Override
    //查询该id用户
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    //行数
    public int findByLine() {
        return userMapper.findByLine();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    //前端修改用户密码
    public void updateForePwd(Object o,String pwd) {
        PasswordEncoder pe = new BCryptPasswordEncoder();
        String s = pe.encode(pwd);
        userMapper.updateForePwd(o,s);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    //前端修改用户头像
    public String updateForeImg(MultipartFile file, LargeFile largeFile, Object o) {
        String uuid = "";
        if (file != null ) {
            try{
                //这里要限制UUID的长度
                String s= UUID.randomUUID().toString();
                String[] id=s.toString().split("-");
                for (int i = 0; i <5; i++) {
                    uuid = uuid+id[i];//32位的UUiD
                }
                largeFile.setFilename(file.getOriginalFilename());
                largeFile.setId(uuid);
                byte[] bytes = file.getBytes();
                largeFile.setContent(bytes);
            }catch(Exception e){
                System.out.println(e);
            }
            User u=(User)o;//获取id用
            User user1 = userMapper.findById(u.getId());
            largeFileService.delById(user1.getImg());
            largeFileService.add(largeFile);
        }
        userMapper.updateForeImg(o,uuid);
        return uuid;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //修改用户信息
    public void updateInfo(Object o, String nickname, String phone) {
        userMapper.updateInfo(o,nickname, phone);
    }
}
